package com.app.couponsharelife.ui.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.app.couponsharelife.data.bean.*
import com.app.couponsharelife.data.remote.RemoteDataSource

/**
 * Created by guluwa on 2018/12/2.
 */
class HeadLineViewModel : BaseViewModel() {

    private var getTaoKouLingFresh = MutableLiveData<FreshBean>()

    private var getTaoKouLingResult: LiveData<ViewDataBean<RemoteResultBean<String>>>? = null

    fun getTaoKouLing(): LiveData<ViewDataBean<RemoteResultBean<String>>>? {
        if (getTaoKouLingResult == null) {
            getTaoKouLingResult = Transformations.switchMap(getTaoKouLingFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getTaoKouLing(it.map)
                } else {
                    null
                }
            }
        }
        return getTaoKouLingResult!!
    }

    fun freshGetTaoKouLing(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getTaoKouLingFresh.value = FreshBean(map, isFresh)
    }

    private var getCircleShareFresh = MutableLiveData<FreshBean>()

    private var getCircleShareResult: LiveData<ViewDataBean<ResultBean<ShareCircleBean>>>? = null

    fun getCircleShare(): LiveData<ViewDataBean<ResultBean<ShareCircleBean>>>? {
        if (getCircleShareResult == null) {
            getCircleShareResult = Transformations.switchMap(getCircleShareFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().getCircleShare(it.map)
                } else {
                    null
                }
            }
        }
        return getCircleShareResult!!
    }

    fun freshGetCircleShare(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) getCircleShareFresh.value = FreshBean(map, isFresh)
    }

    private var circleShareSuccessFresh = MutableLiveData<FreshBean>()

    private var circleShareSuccessResult: LiveData<ViewDataBean<ResultBean<Any>>>? = null

    fun circleShareSuccess(): LiveData<ViewDataBean<ResultBean<Any>>>? {
        if (circleShareSuccessResult == null) {
            circleShareSuccessResult = Transformations.switchMap(circleShareSuccessFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().circleShareSuccess(it.map)
                } else {
                    null
                }
            }
        }
        return circleShareSuccessResult!!
    }

    fun freshCircleShareSuccess(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) circleShareSuccessFresh.value = FreshBean(map, isFresh)
    }

    private var saveShareItemInfoFresh = MutableLiveData<FreshBean>()

    private var saveShareItemInfoResult: LiveData<ViewDataBean<RemoteResultBean<String>>>? = null

    fun saveShareItemInfo(): LiveData<ViewDataBean<RemoteResultBean<String>>>? {
        if (saveShareItemInfoResult == null) {
            saveShareItemInfoResult = Transformations.switchMap(saveShareItemInfoFresh) {
                if (it.isFresh) {
                    RemoteDataSource.getInstance().saveShareItemInfo(it.map)
                } else {
                    null
                }
            }
        }
        return saveShareItemInfoResult!!
    }

    fun freshSaveShareItemInfo(map: HashMap<String, String>, isFresh: Boolean) {
        if (!judgeUser(map)) saveShareItemInfoFresh.value = FreshBean(map, isFresh)
    }
}